package graduate.cslg.com.universityshopping.dormitory;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import graduate.cslg.com.universityshopping.R;
import graduate.cslg.com.universityshopping.net.LoginResponse;
import graduate.cslg.com.universityshopping.net.RetrofitDevelop;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class DetentionActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText stuTel;
    private EditText contract;
    private EditText contractTel;
    private EditText timeStart;
    private EditText timeEnd;
    private EditText stuReason;
    private Button submitButton;
    private String getUserAccount;
    private LoginResponse mLoginResponse;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detention);

        SharedPreferences preferences = this.getSharedPreferences("user_info",MODE_PRIVATE);
        getUserAccount = preferences.getString("user_account", "Z09315222");

        initViews();
    }

    private void initViews() {
        stuTel = findViewById(R.id.stu_tel);
        contract = findViewById(R.id.contract_name);
        contractTel = findViewById(R.id.contract_tel);
        timeStart = findViewById(R.id.time_start);
        timeEnd = findViewById(R.id.time_end);
        stuReason = findViewById(R.id.stu_reason);
        submitButton = findViewById(R.id.submit_button);
        submitButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.submit_button:
                String getStuTel = stuTel.getText().toString();
                String getContractName = contract.getText().toString();
                String getContractTel = contractTel.getText().toString();
                String getTimeStart = timeStart.getText().toString();
                String getTimeEnd = timeEnd.getText().toString();
                String getReason = stuReason.getText().toString();
                if (getStuTel.length() != 0 && getContractName.length() != 0 && getContractTel.length() != 0 && getTimeStart.length() != 0 && getTimeEnd.length() != 0 && getReason.length() != 0) {
                    postDetentionInfo(getStuTel, getContractName, getContractTel, getTimeStart, getTimeEnd, getReason);
                }
                break;
        }
    }

    private void postDetentionInfo(String stuTel, String contractName, String contractTel, String timeStart, String timeEnd, String stuReason) {
        RetrofitDevelop.getTestService()
                .addDetentionInfo(getUserAccount, stuTel, contractName, contractTel, timeStart, timeEnd, stuReason)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LoginResponse>() {
                    @Override
                    public void onCompleted() {
                        if (mLoginResponse.getStatus().equals("true")) {
                            Toast.makeText(DetentionActivity.this, "提交成功！", Toast.LENGTH_SHORT).show();
                            finish();
                        } else {
                            Toast.makeText(DetentionActivity.this, "信息提交失败！", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        Toast.makeText(DetentionActivity.this, "网络错误！", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onNext(LoginResponse loginResponse) {
                        mLoginResponse = loginResponse;
                    }
                });
    }
}
